package com.tencent.qcloud.tim.uikit.modules.group.member;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.noober.background.drawable.DrawableCreator;
import com.tencent.imsdk.v2.V2TIMGroupMemberFullInfo;
import com.tencent.qcloud.tim.uikit.R;
import com.tencent.qcloud.tim.uikit.component.picture.imageEngine.impl.GlideEngine;
import com.tencent.qcloud.tim.uikit.utils.BackgroundTasks;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import com.manor.common.utils.DpUtil;
import com.manor.common.utils.KLog;

public class GroupMemberDeleteAdapter extends BaseQuickAdapter<GroupMemberInfo, BaseViewHolder> {
    private List<GroupMemberInfo> mDelMembers = new ArrayList<>();
    private OnSelectChangedListener mSelectCallback;

    public GroupMemberDeleteAdapter(int layoutResId, @Nullable List<GroupMemberInfo> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NotNull final BaseViewHolder holder, final GroupMemberInfo item) {
        if (!TextUtils.isEmpty(item.getIconUrl()))
            GlideEngine.loadCircleImage((ImageView) holder.getView(R.id.group_member_icon), item.getIconUrl());
        holder.setText(R.id.group_member_name, item.getNickName());

        switch (item.getMemberType()) {
            case V2TIMGroupMemberFullInfo.V2TIM_GROUP_MEMBER_ROLE_OWNER:
                holder.setVisible(R.id.tvTag, true);
                holder.setText(R.id.tvTag, "群主");
                holder.getView(R.id.tvTag).setBackground(new DrawableCreator.Builder()
                        .setCornersRadius(DpUtil.dp2px(8))
                        .setSolidColor(Color.parseColor("#FFD6AC19"))
                        .build());
                break;
            case V2TIMGroupMemberFullInfo.V2TIM_GROUP_MEMBER_ROLE_ADMIN:
                holder.setVisible(R.id.tvTag, true);
                holder.setText(R.id.tvTag, "管理员");
                holder.getView(R.id.tvTag).setBackground(new DrawableCreator.Builder()
                        .setCornersRadius(DpUtil.dp2px(8))
                        .setSolidColor(Color.parseColor("#FF25B22E"))
                        .build());
                break;
        }

        holder.getView(R.id.llLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) holder.getView(R.id.group_member_del_check)).isChecked()){
                    mDelMembers.remove(item);
                    ((CheckBox) holder.getView(R.id.group_member_del_check)).setChecked(false);
               } else {
                    mDelMembers.add(item);
                    ((CheckBox) holder.getView(R.id.group_member_del_check)).setChecked(true);
               }
                if (mSelectCallback != null) {
                    mSelectCallback.onSelectChanged(mDelMembers);
                }
               // notifyItemChanged(holder.getAdapterPosition());
            }
        });

    }


    public void setDataSource(List<GroupMemberInfo> members) {
        if (members != null) {
            KLog.e("deleteMember#setDataSource", members);
            setNewInstance(members);
            BackgroundTasks.getInstance().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();
                }
            });
        }
    }

    public void setOnSelectChangedListener(OnSelectChangedListener callback) {
        mSelectCallback = callback;
    }

    public void clear() {
        mDelMembers.clear();
    }

    public interface OnSelectChangedListener {
        void onSelectChanged(List<GroupMemberInfo> mDelMembers);
    }

}


//public class GroupMemberDeleteAdapter extends BaseAdapter {
//
//    private List<GroupMemberInfo> mGroupMembers = new ArrayList<>();
//    private List<GroupMemberInfo> mDelMembers = new ArrayList<>();
//    private OnSelectChangedListener mSelectCallback;
//
//    public GroupMemberDeleteAdapter() {
//    }
//
//    @Override
//    public int getCount() {
//        return mGroupMembers.size();
//    }
//
//    @Override
//    public GroupMemberInfo getItem(int i) {
//        return mGroupMembers.get(i);
//    }
//
//    @Override
//    public long getItemId(int i) {
//        return 0;
//    }
//
//    @Override
//    public View getView(final int i, View view, final ViewGroup viewGroup) {
//        final MyViewHolder holder;
//        if (view == null) {
//            view = LayoutInflater.from(TUIKit.getAppContext()).inflate(R.layout.group_member_del_adpater,
//            viewGroup, false);
//            holder = new MyViewHolder();
//            holder.llLay = view.findViewById(R.id.llLay);
//            holder.memberIcon = view.findViewById(R.id.group_member_icon);
//            holder.memberName = view.findViewById(R.id.group_member_name);
//            holder.tvTag = view.findViewById(R.id.tvTag);
//            holder.delCheck = view.findViewById(R.id.group_member_del_check);
//            view.setTag(holder);
//        } else {
//            holder = (MyViewHolder) view.getTag();
//        }
//        final GroupMemberInfo info = getItem(i);
//        if (!TextUtils.isEmpty(info.getIconUrl()))
//            GlideEngine.loadCircleImage(holder.memberIcon, info.getIconUrl());
//        holder.memberName.setText(info.getNickName());
//        holder.delCheck.setChecked(false);
//
//        switch (info.getMemberType()) {
//            case V2TIMGroupMemberFullInfo.V2TIM_GROUP_MEMBER_ROLE_OWNER:
//                holder.tvTag.setVisibility(View.VISIBLE);
//                holder.tvTag.setText("群主");
//                holder.tvTag.setBackground(new DrawableCreator.Builder()
//                        .setCornersRadius(DpUtil.dp2px(8))
//                        .setSolidColor(Color.parseColor("#FFD6AC19"))
//                        .build());
//                break;
//            case V2TIMGroupMemberFullInfo.V2TIM_GROUP_MEMBER_ROLE_ADMIN:
//                holder.tvTag.setVisibility(View.VISIBLE);
//                holder.tvTag.setText("管理员");
//                holder.tvTag.setBackground(new DrawableCreator.Builder()
//                        .setCornersRadius(DpUtil.dp2px(8))
//                        .setSolidColor(Color.parseColor("#FF25B22E"))
//                        .build());
//                break;
//        }
//
//
//
////        holder.delCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
////            @Override
////            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
////                if (isChecked) {
////                    mDelMembers.add(info);
////                } else {
////                    mDelMembers.remove(info);
////                }
////                if (mSelectCallback != null) {
////                    mSelectCallback.onSelectChanged(mDelMembers);
////                }
////            }
////        });
//
//        holder.llLay.setClickable(true);
//        holder.llLay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ToastUtil.toastLongMessage("cccccccccccccccccc");
//               if (holder.delCheck.isChecked()){
//                   mDelMembers.add(info);
//               } else {
//                   mDelMembers.remove(info);
//               }
//                if (mSelectCallback != null) {
//                    mSelectCallback.onSelectChanged(mDelMembers);
//                }
//            }
//        });
//        return view;
//    }
//
//    public void setDataSource(List<GroupMemberInfo> members) {
//        if (members != null) {
//            this.mGroupMembers = members;
//            BackgroundTasks.getInstance().runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    notifyDataSetChanged();
//                }
//            });
//        }
//    }
//
//    public void setOnSelectChangedListener(OnSelectChangedListener callback) {
//        mSelectCallback = callback;
//    }
//
//    public void clear() {
//        mDelMembers.clear();
//    }
//
//    public interface OnSelectChangedListener {
//        void onSelectChanged(List<GroupMemberInfo> mDelMembers);
//    }
//
//    private class MyViewHolder {
//        private LinearLayout llLay;
//        private ImageView memberIcon;
//        private TextView memberName;
//        private TextView tvTag;
//        private CheckBox delCheck;
//    }
//}
